//
//  ZXXEmoticonKeyboardView.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/25.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit


///emoticonKeyboardToolBar的高度
let EmoticonKeyboardToolBarHeight: CGFloat = 37

class ZXXEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(emoticonKeyboardToolBar)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        //添加约束
        emoticonKeyboardToolBar.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(EmoticonKeyboardToolBarHeight)
        }
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emoticonKeyboardToolBar.snp_top)
        }
        pageControl.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView)
        }
        
        //确定点击了某一个按钮
        emoticonKeyboardToolBar.ZXXEmoticonToolBarClosure = {[weak self] (type: ZXXEmoticonToolBarButtonType) in
            //滚动到指定的位置
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            
            // pageControl 的总数
            self?.pageControl.numberOfPages = ZXXEmoticonTools.sharedZXXEmoticonTools.allEmoticons[indexPath.section].count
            // 当前选中的
            self?.pageControl.currentPage = 0
        }
    }
    //  MARK: -  懒加载控件
    /// 表情按钮 toolbar
    private lazy var emoticonKeyboardToolBar: ZXXEmoticonToolBar  = ZXXEmoticonToolBar()
    ///表情显示的collectionView
    private lazy var emoticonPageView: ZXXEmoticonPageView = {
        let view = ZXXEmoticonPageView()
        //取消弹簧效果
        view.bounces = false
        //取消滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        //分页效果
        view.pagingEnabled = true
        //分页显示当前页 需要滚动得代理
        view.delegate = self
       return view
    }()
    /// pageControl
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        // 总页数
        pageControl.numberOfPages = ZXXEmoticonTools.sharedZXXEmoticonTools.allEmoticons[0].count
        // 当前选中的页数
        pageControl.currentPage = 0
        //        pageControl.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //        pageControl.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        // kvc 给私有属性设置image
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        return pageControl
    }()


}

extension ZXXEmoticonKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 得到相对于我们scrollView的x 中心点
        let centerX = SCREENWIDTH / 2  + scrollView.contentOffset.x
        // 给数组排序
        let cells =  emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        // 只有屏幕显示两个cell的时候我们才会切换选中的按钮
        if cells.count == 2{
            var indexPath: NSIndexPath?
            
            let firstCell = cells.first!
            let lastCell = cells.last!
            // 判断我们当前的cell 的frame 是否包含 一个点坐标
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
            }else{
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
            }
            
            // 设置选中按钮状态
            emoticonKeyboardToolBar.setEmoticonCurrentButton(indexPath!.section)
            // pageControl 的总数
            pageControl.numberOfPages = ZXXEmoticonTools.sharedZXXEmoticonTools.allEmoticons[indexPath!.section].count
            // 当前选中的
            pageControl.currentPage = indexPath!.item
        }
        
        
    }
}

